package exportimporterentities;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;

import org.apache.log4j.Logger;

import autoservice.api.entitites.IGarage;
import autoservice.api.entitites.IMaster;
import autoservice.api.entitites.IOrder;
import autoservice.api.messages.Messager;
import entitiesfactory.EntitiesFactory;

public class Import {
	private static final String DELIMITER = " ";
	private static final Logger log= Logger.getLogger(Import.class);
	private Scanner garageReader;
	private Scanner masterReader;
	private Scanner orderReader;	
	private ArrayList<IOrder> arrayOrder = new ArrayList<IOrder>();
	private ArrayList<IMaster> arrayMaster = new ArrayList<IMaster>();
	private ArrayList<IGarage> arrayGarage = new ArrayList<IGarage>();	
	private EntitiesFactory entities_factory=EntitiesFactory.getInstance();

	

	ArrayList<IGarage> getGarages(String path) throws FileNotFoundException {
		garageReader = new Scanner(new File(path));
		String temp[] = new String[2];
		while (garageReader.hasNextLine()) {
			String s = garageReader.nextLine();
			temp = s.split(DELIMITER);
			IGarage garage =(IGarage) entities_factory.getObject(IGarage.class);
			garage.setIdGarage(Integer.parseInt(temp[0]));
			garage.setNumber(Integer.parseInt(temp[1]));
			garage.setFullness(Boolean.parseBoolean(temp[2]));
			arrayGarage.add(garage);
		}
		return arrayGarage;
		
	}

	ArrayList<IMaster> getMasters(String path) throws FileNotFoundException {
		masterReader = new Scanner(new File(path));
		String temp[] = new String[2];
		while (masterReader.hasNextLine()) {
			String s = masterReader.nextLine();
			temp = s.split(DELIMITER);
			IMaster master = (IMaster) entities_factory.getObject(IMaster.class);
			master.setId(Integer.parseInt(temp[0]));
			master.setName(temp[1]);
			master.setEmployment(Boolean.parseBoolean(temp[2]));
			arrayMaster.add(master);
		}

		masterReader.close();
		return arrayMaster;
	}

	ArrayList<IOrder> getOrders(String orderPath,String masterPath,String garagePath) throws FileNotFoundException {
		orderReader = new Scanner(new File(orderPath));
		String temp[] = new String[8];
		HashMap<Integer, Integer> masterTemp = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> garageTemp = new HashMap<Integer, Integer>();
		while (orderReader.hasNextLine()) {
			String s = orderReader.nextLine();
			temp = s.split(DELIMITER);
			IOrder order = (IOrder) entities_factory.getObject(IOrder.class);
			order.setIdOrder(Integer.parseInt(temp[0]));
			order.setNumber(Integer.parseInt(temp[1]));
			order.setFillingDate(parser(temp[2]));
			order.setDeadlineDate(parser(temp[3]));
			order.setBeginingDate(parser(temp[4]));
			order.setPrice(Integer.parseInt(temp[5]));
			order.setOrderStatus(temp[6]);			
			arrayOrder.add(order);
			masterTemp.put(order.getNumber(), Integer.parseInt(temp[7]));
			garageTemp.put(order.getNumber(), Integer.parseInt(temp[8]));

		}

		joinData(masterTemp, garageTemp,masterPath,garagePath);
		orderReader.close();
		return arrayOrder;
	}

	private Date parser(String h) {
		SimpleDateFormat f = new SimpleDateFormat(Messager.DATE_FORMAT);
		Date date= new Date();
		try {
		  date = f.parse(h);
		} catch (ParseException pe) {
			log.error(Messager.WRONG_DATE_FORMAT);
			throw new RuntimeException(Messager.WRONG_DATE_FORMAT);
		}
		return date;

	}

	private void joinData(HashMap<Integer, Integer> masterTemp,
			HashMap<Integer, Integer> garageTemp, String masterPath, String garagePath) throws FileNotFoundException {
		getGarages(garagePath);
		getMasters(masterPath);
		for (IOrder order : arrayOrder) {
			for (IMaster master : arrayMaster) {
				for (IGarage garage : arrayGarage) {
					if (masterTemp.get(order.getNumber()) == master.getId()
							&& garageTemp.get(order.getNumber()) == garage
									.getId()) {
						order.setMaster(master);
						order.setGarage(garage);
					}
				}
			}
		}
	}
	

}
